<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="roleInfo">
                  <el-button size="small" type="success" @click="sendPermission(roleInfo.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(roleInfo.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(roleInfo.row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :page-size="pageConfig.pagesize"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input disabled style="width:400px" :value="companyInfo.name" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled style="width:400px" :value="companyInfo.companyAddress" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled style="width:400px" :value="companyInfo.mailbox" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" :rows="3" disabled style="width:400px" :value="companyInfo.remarks" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <!-- 编辑与新增的弹窗 -->
      <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限的弹窗 -->
      <el-dialog title="分配权限" :visible="isShowPermission" @close="closeFn">
        <el-tree
          ref="permissionTree"
          :data="permissionList"
          :props="{label:'name'}"
          show-checkbox
          default-expand-all
          check-strictly
          node-key="id"
        />
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button size="small" type="primary" @click="permissionOK">确定</el-button>
            <el-button size="small">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyRole, getCompanyInfo, delCompanyRole, addCompanyRole, getRoleDetail, editCompanyRole } from '@/api/compamySetting'
import { assignPermission, getPermission } from '@/api/permission'
import { toTreeData } from '@/utils'
export default {
  data() {
    return {
      roleId: '',
      permissionList: [],
      isShowPermission: false,
      roleList: [],
      companyInfo: {},
      pageConfig: {
        page: 1,
        pagesize: 3
      },
      total: 0,
      isShowDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.getCompanyRole()
    // 获取公司信息
    this.companyInfo = await getCompanyInfo(this.$store.getters.companyId)
    // console.log(this.companyInfo)
    // 获取所有权限
    this.getPermission()
  },
  methods: {
  // 权限确认按钮
    async  permissionOK() {
      // console.log(666)
      console.log(this.roleId, this.$refs.permissionTree.getCheckedKeys())
      await assignPermission({ id: this.roleId,
        permIds: this.$refs.permissionTree.getCheckedKeys() })
      this.$message.success('分配成功')
      this.isShowPermission = false
    },
    // 获取所有权限
    async getPermission() {
      this.permissionList = toTreeData(await getPermission(), '0')
      // console.log(this.permissionList)
    },
    // 关闭分配权限弹窗
    closeFn() {
      this.isShowPermission = false
    },
    // 点击分配权限
    async  sendPermission(id) {
      const res = await getRoleDetail(id)
      this.roleId = id
      // console.log(res)
      this.isShowPermission = true
      this.$nextTick(() => {
        // 用于回显多选框，需要搭配node-key使用
        this.$refs.permissionTree.setCheckedKeys(res.permIds)
      })
    },
    // 编辑角色
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.isShowDialog = true
    },

    // 新增角色
    addRole() {
      this.isShowDialog = true
    },
    // 取消
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.isShowDialog = false
    },
    // 确认
    async btnOK() {
      await this.$refs.roleForm.validate()
      if (this.roleForm.id) {
        // 编辑
        await editCompanyRole(this.roleForm)
      } else {
        // 新增
        await addCompanyRole(this.roleForm)
      }

      this.$message.success('操作成功')
      this.isShowDialog = false
      // 更新页面
      this.getCompanyRole()
    },

    // 获取公司角色
    async getCompanyRole() {
      const res = await getCompanyRole(this.pageConfig)
      // console.log(res)
      this.roleList = res.rows
      this.total = res.total
      // console.log(this.total)
    },
    // 页面切换
    changePage(currentPage) {
      this.pageConfig.page = currentPage
      this.getCompanyRole()
    },
    async delRole(id) {
      // 二次确认
      await this.$confirm('是否确认删除')
      // 发起请求
      await delCompanyRole(id)
      // 提示信息
      this.$message.success('删除成功')
      // 更新页面
      this.getCompanyRole()
    }
  }
}
</script>

<style>

</style>
